using System;
using Scenario.WorkflowEngine.DataModel;

namespace Scenario.WorkflowEngine.DataModel.WorkflowProcesses
{
	//*********************************************************************
	//
	// Transition Class
	//
	// The XmlTransition class represents, in a workflow template, how to
	// jump information flow from an activity to an other ativity.
	// Every WF contains at least one transition from start activity to end.
	//
	//*********************************************************************

	public class XMLTransition : bOS.XML.Data.XMLObject 
	{
		public const string MAIN_NODE	= "Transition";

		private const string FROM_ATTRIBUTE	= "From";
		private const string TO_ATTRIBUTE	= "To";
		private const string RESULT_NODE	= "Result";
		private const string REEXEC_NODE	= "ReExec";
		private const string OPERATOR_NODE	= "Operator";

		public const string YES_REEXEC		= "YES";
		public const string NO_REEXEC		= "NO";

		public const string NOTEQUAL_OPERATOR			= "NotEqual";
		public const string EQUAL_OPERATOR				= "Equal";
		public const string LESS_THAN_OPERATOR			= "LessThan";
		public const string MORE_THAN_OPERATOR			= "GreatThan";
		public const string LESS_EQUAL_THAN_OPERATOR	= "LessEqualThan";
		public const string MORE_EQUAL_THAN_OPERATOR	= "GreatEqualThan";
		public const string ALWAYS_OPERATOR				= "Always";

		public const string DEFAULT_REEXEC		= "YES";
		public const string DEFAULT_RESULT		= "[None]";
		public const string DEFAULT_OPERATOR	= "Always";

		private string sFrom;
		private string sTo;
		private string sResult;
		private string sReExec;
		private string sOperator;

		/** CONSTRUCTORS **/

		/** CONSTRUCTOR Width simple Property **/
		public XMLTransition()
		{
			this.Init();
		}

		/** CONSTRUCTOR Width xml parsering**/
		public XMLTransition(System.Xml.XmlNode node)
		{
			this.Parse(node);
		}

		/** PROPERTIES **/

		public string From 
		{
			get { return sFrom; }
			set { sFrom = value; }
		}

		public string To 
		{
			get { return sTo; }
			set { sTo = value; }
		}

		public string Result 
		{
			get { return sResult; }
			set { sResult = value; }
		}

		public string ReExec 
		{
			get { return sReExec; }
			set { sReExec = value; }
		}

		public string Operator 
		{
			get { return sOperator; }
			set { sOperator = value; }
		}

		/** INSTANCE METHODS **/

		/** Inizializing method **/
		override public void Init()
		{
			sFrom= string.Empty;
			sTo= string.Empty;
			sResult= DEFAULT_RESULT;
			sReExec= DEFAULT_REEXEC;
			sOperator= DEFAULT_OPERATOR;
		}

		/** Parsering method for filling the transition **/
		override public void Parse(System.Xml.XmlNode node2Parse)
		{
			this.Init();

			System.Xml.XmlNode node;

			/** find the attributes situated on the root tag **/
			for ( int i=0; i<node2Parse.Attributes.Count;i++)
			{
				node = node2Parse.Attributes.Item(i);

				switch ( node.Name )
				{
					case XMLTransition.FROM_ATTRIBUTE:
						sFrom = node.InnerText;
						break;
					case XMLTransition.TO_ATTRIBUTE:
						sTo = node.InnerText;
						break;
				}
			}

			/** find the sub tags witch contains values **/
			for ( int i=0; i<node2Parse.ChildNodes.Count; i++)
			{
				node = node2Parse.ChildNodes[i];

				switch ( node.Name )
				{
					case XMLTransition.RESULT_NODE:
						sResult = node.InnerText;
						break;
					case XMLTransition.REEXEC_NODE:
						sReExec = node.InnerText;
						break;
					case XMLTransition.OPERATOR_NODE:
						sOperator = node.InnerText;
						break;
				}
			}
		}

		override public bOS.XML.XMLNode GetXMLNode()
		{
			bOS.XML.XMLNode xmlNode = new bOS.XML.XMLNode(XMLTransition.MAIN_NODE);
			xmlNode.AddAttribute ( new bOS.XML.XMLAttribute (XMLTransition.FROM_ATTRIBUTE, sFrom) );
			xmlNode.AddAttribute ( new bOS.XML.XMLAttribute (XMLTransition.TO_ATTRIBUTE, sTo) );
			xmlNode.AddNode ( new bOS.XML.XMLNode (XMLTransition.RESULT_NODE,sResult));
			xmlNode.AddNode ( new bOS.XML.XMLNode (XMLTransition.REEXEC_NODE,sReExec));
			xmlNode.AddNode ( new bOS.XML.XMLNode (XMLTransition.OPERATOR_NODE,sOperator));
			
			return xmlNode;
		}

		public void Set2Default()
		{
			this.sResult = XMLTransition.DEFAULT_RESULT;
			this.sReExec = XMLTransition.DEFAULT_REEXEC;
			this.sOperator = XMLTransition.DEFAULT_OPERATOR;
		}

		/** STATIC METHODS **/

		/** no static methods **/
	}
}